package com.xiao.dynamic_rule.utils;

import com.mysql.cj.util.StringUtils;
import com.xiao.dynamic_rule.pojo.LogBean;
import com.xiao.dynamic_rule.pojo.RuleParamAtomic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RuleCalcUtil {
    /**
     * 工具方法：判断state和规则条件中的原子条件是否一致
     *
     * @param eventBean
     * @param eventParam
     * @return
     */
    public static boolean eventBeanMatchEventParam(LogBean eventBean, RuleParamAtomic eventParam) {
        if (eventBean.getEventId().equals(eventParam.getEventId())) {
            Map<String, String> eventProperties = eventBean.getProperties();
            //取出条件中的事件属性
            HashMap<String, String> paramProperties = eventParam.getProperties();

            //遍历条件中的每一个属性和值
            Set<Map.Entry<String, String>> entries = paramProperties.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (!entry.getValue().equals(eventProperties.get(entry.getKey()))) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static boolean eventBeanMatchEventParam(LogBean eventBean, RuleParamAtomic eventParam, boolean needTimeCompare) {
        boolean result = eventBeanMatchEventParam(eventBean, eventParam);

        //考虑外部传入的时间起始没有约束则应该传入 -1
        long start = eventParam.getRangeStart();
        long end = eventParam.getRangeEnd();

        return result && eventBean.getTimeStamp() >= (start == -1 ? 0 : start) && eventBean.getTimeStamp() <= (end == -1 ? Long.MAX_VALUE : end);
    }

    /**
     * 将传入的规则参数拼接为redis中的key
     * @param deviceId 用户唯一id
     * @param ruleParamAtomic 传入的规则条件
     * @return String key
     */
    public static String getBufferKey(String deviceId, RuleParamAtomic ruleParamAtomic) {

        // deviceId|eventId-p1-v1-p2-v2
        StringBuffer sb = new StringBuffer();
        sb.append(deviceId).append("|").append(ruleParamAtomic.getEventId());

            HashMap<String, String> properties = ruleParamAtomic.getProperties();
            Set<Map.Entry<String, String>> entries = properties.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                sb.append("-").append(entry.getKey()).append("-").append(entry.getValue());
        }

        return sb.toString();
    }

    /**
     * 将传入的规则参数拼接为redis中的key
     * @param deviceId 用户唯一id
     * @param userActionCountParams 传入的规则条件list
     * @return String key
     */
    public static String getBufferKey(String deviceId, List<RuleParamAtomic> userActionCountParams) {

        // deviceId|eventId-p1-v1-p2-v2
        StringBuffer sb = new StringBuffer();
        sb.append(deviceId);

        for (RuleParamAtomic userActionCountParam : userActionCountParams) {
            sb.append("|").append(userActionCountParam.getEventId());

            HashMap<String, String> properties = userActionCountParam.getProperties();
            Set<Map.Entry<String, String>> entries = properties.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                sb.append("-").append(entry.getKey()).append("-").append(entry.getValue());
            }
        }

        return sb.toString();
    }

}
